import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
let state={
    count:10
}

//处理状态数据变化
const mutations={
    increment(state){
        state.count++
    },
    decrement(state){
        state.count--
    },
    async clickAsync(state){
        await setTimeout(function() {
                state.count=0
                alert()
        }, 3000);
    }
}

//处理你要做什么，异步请求，判断，流程控制
const actions={
    increment:({commit})=>{
        commit('increment')
    },
    decrement:({commit})=>{
        commit('decrement')
    },
    clickOdd:({commit,state})=>{
        console.log(state.count)
        if(state.count%2==0){
            commit('increment')
        }
    },
    clickAsync:({commit,state})=>{
        commit('clickAsync')
    }
}

const getters={
    count(state){
        return state.count
    }
}

export default new Vuex.Store({
    state,
    mutations,
    actions,
    getters
})